package com.xkdx.guangguang.fragment.my.pay;

import java.util.Vector;

public class TCPSocketConnect implements Runnable {

	private boolean isWrite = false;// 是否发送消息头数据
	private boolean isBodyWrite = false; // 是否发送消息体数据
	private static Vector<byte[]> datas = new Vector<byte[]>();// 待发送数据队列消息头
	private static Vector<String> datasBody = new Vector<String>();// 待发送数据队列消息体
	private TCPSocketFactory mSocket;// socket连接
	private WriteRunnable writeRunnable;// 发送数据线程
	private String ip = null;
	private int port = -1;

	/**
	 * 创建连接
	 * 
	 * @param callback
	 *            回调接口
	 * @param executor
	 *            线程池对象
	 */
	public TCPSocketConnect(TCPSocketCallback callback) {
		mSocket = new TCPSocketFactory(callback);// 创建socket连接
		writeRunnable = new WriteRunnable();// 创建发送线程
	}

	@Override
	public void run() {
		if (ip == null || port == -1) {
			return;
		}

		try {
			mSocket.connect(ip, port);// 连接服务器
		} catch (Exception e) {

			resetConnect();// 断开连接
		}

		isWrite = true;// 设置可发送消息头数据
		isBodyWrite = true; // 设置可发送消息体数据
		new Thread(writeRunnable).start();// 启动发送线程

		try {
			mSocket.read();// 获取数据
		} catch (Exception e) {

		} finally {
			resetConnect();// 断开连接
		}
	}

	/**
	 * 关闭服务器连接
	 */
	public void disconnect() {

		resetConnect();
	}

	/**
	 * 重置连接
	 */
	public void resetConnect() {

		writeRunnable.stop();// 发送停止信息
		writeRunnable.stopBody();// 发送停止信息
		mSocket.disconnect();
	}

	/**
	 * 向发送线程写入发送数据
	 */
	public void write(byte[] buffer) {
		writeRunnable.write(buffer);
	}

	/**
	 * 向发送线程写入发送数据
	 */
	public void writeBody(String body) {

		writeRunnable.writeBody(body);
	}

	/**
	 * 设置IP和端口
	 * 
	 * @param ip
	 * @param port
	 */
	public void setAddress(String host, int port) {
		this.ip = host;
		this.port = port;
	}

	/**
	 * 发送数据
	 */
	private boolean writes(byte[] buffer) {
		try {
			// byte[] buffer0 = buffer.getBytes();
			mSocket.write(buffer);
			Thread.sleep(1);
			writeRunnable.stop();// 发送停止信息
			
			return true;
		} catch (Exception e) {
			resetConnect();
			return false;
		}
	}

	/**
	 * 发送数据
	 */
	private boolean writesBody(String buffer) {
		try {

			mSocket.writeBody(buffer);
			Thread.sleep(1);
			writeRunnable.stopBody();// 发送停止信息
			return true;
		} catch (Exception e) {
			resetConnect();
			return false;
		}
	}

	/**
	 * 发送线程
	 * 
	 * @author
	 */
	private class WriteRunnable implements Runnable {

		@Override
		public void run() {
			while (isWrite) {
				while (datas.size() > 0) {
					byte[] buffer = datas.remove(0);// 获取一条发送数据
					if (isWrite) {
						writes(buffer);// 发送数据
						isWrite = false;
					}
				}
			}

			while (isBodyWrite) {
				while (datasBody.size() > 0) {
					String buffer = datasBody.remove(0);// 获取一条发送数据
					if (isBodyWrite) {
						writesBody(buffer);// 发送数据
						isBodyWrite = false;
					}
				}
			}

		}

		/**
		 * 添加数据到发送队列
		 * 
		 * @param buffer
		 *            数据字节
		 */
		public void write(byte[] buffer) {
			datas.add(buffer);// 将发送数据添加到发送队列
		}

		public void writeBody(String buffer) {
			datasBody.add(buffer);// 将发送数据添加到发送队列
		}

		public void stop() {
			isWrite = false;
		}

		public void stopBody() {
			isBodyWrite = false;
		}
	}

}
